package com.fyy.rtspdemo.utils;

import org.omg.CORBA.PUBLIC_MEMBER;

/**
 * 计算坐标距离
 */
public class GoogleUtil {

    //地球半径，单位：m
    private static final double EARTH_RADIUS = 6371000;

    /**
     * 弧度转换
     * @param d
     */
    private static double rad(double d){
        return d * Math.PI / 180.0;
    }

    /**
     * 计算两个坐标的实际距离
     * @param lon1 原点坐标经度
     * @param lat1 原点坐标维度
     * @param lon2 终点坐标经度
     * @param lat2 终点坐标维度
     * @return 实际距离
     */
    public static double computeDistance(double lon1,double lat1,double lon2, double lat2) {
        // 维度转弧度
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        // 计算经度差
        double a = radLat1 - radLat2;
        // 计算经度弧度差
        double b = rad(lon1) - rad(lon2);
        // 计算距离
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    /**
     * 计算两个坐标的实际距离
     * @param centerLon 原点坐标经度
     * @param centerLat 原点坐标维度
     * @param targetLon 终点坐标经度
     * @param targetLat 终点坐标维度
     * @return 实际距离
     */
    private static double distance(double centerLon, double centerLat, double targetLon, double targetLat) {
        // 每经度单位米;
        double jl_jd = 102834.74258026089786013677476285;
        // 每纬度单位米;
        double jl_wd = 111712.69150641055729984301412873;
        double b = Math.abs((centerLat - targetLat) * jl_jd);
        double a = Math.abs((centerLon - targetLon) * jl_wd);
        return Math.sqrt((a * a + b * b));
    }

    public static void main(String[] args) {
        double yLongitude = 113.622337;
        double yDimension = 34.741821;
        double zLongitude = 116.400298;
        double zDimension = 39.899343;
        System.out.println("实际距离1：" + computeDistance(yLongitude,yDimension,zLongitude,zDimension));
        System.out.println("实际距离2：" + distance(yLongitude,yDimension,zLongitude,zDimension));
    }

}
